<?phpnamespace app\api\controller;use app\common\logic\SmsLogic;use think\AjaxPage;use think\Db;use think\Exception;class Administrators extends Base{    /**     * 登录     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function login(){        $user_name = input('post.username/s');        $password = input('post.password/s');        if(empty($user_name)){            return json(array('code' => 500, 'msg' => 'error:请输入用户名'));        }        if(empty($password)){            return json(array('code' => 500, 'msg' => 'error:请输入密码'));        }        $ec_salt = Db::name('user_admin')->where('user_name',$user_name)->getField('ec_salt');        $password = encrypt($password.$ec_salt);        $admin_info = Db::name('user_admin')->where(['user_name'=>$user_name,'password'=>$password])->find();        if($admin_info){            $token = $this->token($user_name,$password);            Db::name('user_admin')->where(['user_name'=>$user_name,'password'=>$password])->update(['token'=>$token]);            $data = [                'user_admin_id' => $admin_info['user_admin_id'],                'role' => $admin_info['role'],                'nickname' => $admin_info['nickname'],                'imges' => $admin_info['imges'],                'token' =>$token,            ];            return json(array('code' => 200, 'msg' => 'success:登录成功', 'data' =>  $data));        }else{            return json(array('code' => 500, 'msg' => 'error:账号密码不正确'));        }    }    /**     * 获取管理信息     * @return \think\response\Json     */    public function manage_information(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $where= [];        if($userInfo['role'] == 2){            $where['sale_id'] = $userInfo['user_admin_id'];        }        $userInfo['notes_to']  =Db::name('notes_to_taken')->where($where)->where('read_type',1)->count();        if($userInfo['role'] == 1){            $userInfo['sale_apply']  =Db::name('sale_apply_list')->where(['examine_type'=>0])->count();        }        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' =>  $userInfo        ));    }    /**     * 管理员帮助预约     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function administratorsSubscribe(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $params = input('param.');        if (empty($params['orderDate'])) {            return json(array('code' => 500, 'msg' => 'error:缺少参数orderDate,未选择预约日期'));        }        if (empty($params['timePeriodType'])) {            return json(array('code' => 500, 'msg' => 'error:缺少参数timePeriodType,未选择预约时间段'));        }        if (empty($params['positionId'])) {            return json(array('code' => 500, 'msg' => 'error:缺少参数positionId,未选择球位'));        }        if (empty($params['userName'])) {            return json(array('code' => 500, 'msg' => 'error:缺少参数userName,未填写预约姓名'));        }        if (empty($params['userPhone'])) {            return json(array('code' => 500, 'msg' => 'error:缺少参数userPhone,未填写预约手机号码'));        }        if (!check_mobile($params['userPhone'])) {            return json(array('code' => 500, 'msg' => 'error:参数userPhone错误,手机号码格式错误'));        }        $position = Db::name('position')->where('positionId', '=', $params['positionId'])->find();        if (empty($position) || $position['positionStatus'] == '0' || $position['positionIsDel'] == '1') {            return json(array('code' => 500, 'msg' => 'error:球位错误,暂不支持进行预约'));        }        $user = Db::name('user')->where('userMobile',$params['userPhone'])->find();        if(empty($user)){            return json(array('code' => 500, 'msg' => 'error:该用户不存在'));        }        $orderList = Db::name('order_subscribe')            ->where('orderStatus', 'in', array(0, 1))            ->where('orderDate', '=', strtotime($params['orderDate']))            ->where('orderPositionId', '=', $position['positionId'])            ->field('orderSubscribeId,orderTimePeriodType,orderDate')            ->select();        $timePeriodType = explode(',', $params['timePeriodType']);        $add=[];        foreach($timePeriodType as $v){            $add[] = config('timePeriod')[$v-1]['startTime'].'-'.config('timePeriod')[$v-1]['endTime'];        }        if (!empty($orderList)) {            foreach ($orderList as &$order) {                $orderTimePeriodType = explode(',', $order['orderTimePeriodType']);                $repeatTimePeriodType = array_intersect($timePeriodType, $orderTimePeriodType);                if (!empty($repeatTimePeriodType)) {                    return json(array('code' => 500, 'msg' => 'error:预约时段中有时段已被其他用户预约'));                }            }        }        $weekarray = array("日", "一" , "二" , "三", "四", "五", "六");        $countTimePeriodType = count($timePeriodType);        $Start = $this->getTimePeriodTypeInfo($timePeriodType[0]);        $orderStartTime = $Start['startTime'];        if ($countTimePeriodType > 0) {            $End = $this->getTimePeriodTypeInfo($timePeriodType[$countTimePeriodType-1]);            $orderEndTime = $End['endTime'];        }else{            $orderEndTime = $Start['endTime'];        }        $catch = array(            'orderSubscribeNo' => 'YY' . date('Ymd', time()) . $userInfo['user_admin_id'] . rand(1000, 9999),            'userId' => $user['userId'],            'orderPrice' => 0,            'orderNote' => !empty($params['orderNote']) ? $params['orderNote'] : "",            'userName' => $params['userName'],            'userPhone' => $params['userPhone'],            'orderTimePeriodType' => $params['timePeriodType'],            'orderTimePeriodTypeLog' => $params['timePeriodType'],            'orderDate' => strtotime($params['orderDate']),            'orderWeek' => '周' . $weekarray[date("w", strtotime($params['orderDate']))],            'orderShopId' => $position['positionShopId'],            'orderPositionId' => $position['positionId'],            'orderPositionName' => "球位" . $position['positionNo'],            'orderStartTime' => $orderStartTime,            'orderEndTime' => $orderEndTime,            'orderAddTime' => time(),            'administrators_id' => $userInfo['user_admin_id'],            'orderStatus' => 1,            'orderPayTime' => time(),            'orderPayName' => 'balance'        );        $orderId = Db::name('order_subscribe')->insertGetId($catch);        if($orderId){            (new Payment())->sendSmsSubscribeSuccess(strtotime($params['orderDate']), $params['timePeriodType'], $position['positionShopId'], $position['positionId'], $params['userPhone']);        }        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' => array(                'orderId' => $orderId,                'timePeriod' =>$add,            ),        ));    }    /**     * 管理端 - 预约订单列表     * @return \think\response\Json     * @throws \think\exception\DbException     */    public function subscribe(){        $params = input('get.');        $page = input('page', '1');        $datetype = input('datetype', '');        $where = array();        if (isset($params['keywords']) && !empty($params['keywords'])) {            $where['userName|userPhone'] = array('like', '%'.$params['keywords'].'%');        }        if (isset($params['orderStatus']) && $params['orderStatus'] != '') {            $where['orderStatus'] = array('=', $params['orderStatus']);        }        if($datetype == 1){            //昨天            $yesterday = strtotime(date('Y-m-d', strtotime("-1 day")));            $where['orderDate'] = $yesterday;        }elseif ($datetype == 2){            //今天            $today     = strtotime(date('Y-m-d'));            $where['orderDate'] = $today;        }elseif ($datetype == 3){            //明天            $tomorrow  = strtotime(date('Y-m-d', strtotime("+1 day")));            $where['orderDate'] = $tomorrow;        }        $list = Db::name('order_subscribe')            ->where($where)            ->order('orderAddTime desc')            ->paginate([                'list_rows'     => 10,                'page' => $page,            ])->toArray();        $timePeriod = config('timePeriod');        foreach ($list['data'] as &$value){            $value['shopName'] = Db::name('shop')->where('shopId', '=', $value['orderShopId'])->getField('shopName');            $value['positionNo'] = Db::name('position')->where('positionId', '=', $value['orderPositionId'])->getField('positionNo');            $value['orderAddTime'] = date('Y-m-d',$value['orderAddTime']);            $value['orderDate'] = date('Y-m-d',$value['orderDate']);            $torderTimePeriodType = explode(',',$value['orderTimePeriodType']);            $add = [];            foreach($torderTimePeriodType as $n=>$m){                $add[$n]['orderTimePeriodType'] =  $m;                $add[$n]['startTime'] =  $timePeriod[$m-1]['startTime'];                $add[$n]['endTime'] =  $timePeriod[$m-1]['endTime'];            }            $value['orderTimePeriod'] = $add;        }        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' =>  $list        ));    }    /**     * 教练列表     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function coachs_list(){        $coachList = Db::name('coachs')->where('coachIsDel', '=', '0')->select();        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' =>  $coachList        ));    }    /**     * 管理端 - 课程订单列表     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function course(){        $params = input('get.');        $page = input('page', '1');        $datetype= input('datetype');        $where = array();        if (isset($params['keywords']) && !empty($params['keywords'])) {            $where['orderCourseName'] = array('like', '%'.$params['keywords'].'%');        }        if (isset($params['coachId']) && !empty($params['coachId'])) {            $where['orderCoachId'] = array('=', $params['coachId']);        }        if (isset($params['orderStatus']) && $params['orderStatus'] != '') {            $where['orderStatus'] = array('=', $params['orderStatus']);        }        if($datetype){            if($datetype == 1){                //昨天                $starttime = strtotime(date('Y-m-d', strtotime("-1 day")).' 0:0:0');                $end=  strtotime(date('Y-m-d', strtotime("-1 day")).' 23:59:59');                $where['orderAddTime'] = ['between',[$starttime,$end]];            }else{                //今天                $starttime = strtotime(date('Y-m-d').' 0:0:0');                $end=  strtotime(date('Y-m-d').' 23:59:59');                $where['orderAddTime'] = ['between',[$starttime,$end]];            }        }        $where['source'] = 0;        $list = Db::name('order_course')            ->where($where)            ->order('orderAddTime desc')            ->paginate([                'list_rows'     => 10,                'page' => $page,            ])->toArray();        foreach ($list['data'] as &$value){            $value['shopName'] = Db::name('shop')->where('shopId', '=', $value['orderShopId'])->getField('shopName');            $user = Db::name('user')->where('userId', '=', $value['userId'])->find();            $value['user'] = $user;            $coach = Db::name('coachs')->where('coachId', '=', $value['orderCoachId'])->find();            $value['coach'] = $coach;            $value['orderAddTime'] = date('Y-m-d',$value['orderAddTime']);        }        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' =>  $list        ));    }    /**     * 管理端 - 课程订单详情     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function getCourseDetail(){        $orderId = input('orderId', '');        $orderInfo = Db::name('order_course')->where('orderCourseId', '=', $orderId)->find();        $orderInfo['user'] = Db::name('user')->where('userId', '=', $orderInfo['userId'])->find();        $orderInfo['shop'] = Db::name('shop')->where('shopId', '=', $orderInfo['orderShopId'])->find();        $orderInfo['coach'] = Db::name('coachs')->where('coachId', '=', $orderInfo['orderCoachId'])->find();        $orderInfo['orderAddTime'] = date('Y-m-d',$orderInfo['orderAddTime']);        $evaluation = Db::name('coach_course_evaluation')->where('orderEvaluationId', '=', 'orderEvaluationId')->find();        $orderInfo['orderEvaluationId'] > 0 ? $orderInfo['evaluation'] = $evaluation : $orderInfo['evaluation'] = array();        !empty($orderInfo['evaluation']) ? $orderEvaluationImages = explode(',', $evaluation['orderEvaluationImages']) : $orderEvaluationImages = "";        $orderLog = Db::name('order_course_log')->where('orderCourseId', '=', $orderInfo['orderCourseId'])->order('logAddTime desc')->select();        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' => array(                'orderLog' => $orderLog,                'orderInfo' =>$orderInfo,                'orderEvaluationImages' =>$orderEvaluationImages,            ),        ));    }    /**     * 管理端 - 商场订单列表     * @return \think\response\Json     * @throws Exception     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     * @throws \think\exception\PDOException     */    public function motion(){        $params = input('get.');        $page = input('page', '1');        $where = array();        if (isset($params['keywords']) && !empty($params['keywords'])) {            $where['orderMotionNo|orderTransactionId'] = array('like', '%'.$params['keywords'].'%');        }        if (isset($params['orderStatus']) && $params['orderStatus'] != '') {            $where['orderStatus'] = array('=', $params['orderStatus']);        }        $motion = Db::name('order_motion')->where($where)->select();        foreach ($motion as $v){            if($v['orderStatus'] == 0){                if(time() >= $v['orderAddTime'] + 600){                    Db::name('order_motion')->where('orderMotionId',$v['orderMotionId'])->update(['orderStatus'=>'-1']);                }            }        }        if($params['datetype']){            if($params['datetype'] == 1){                //昨天                $starttime = strtotime(date('Y-m-d', strtotime("-1 day")).' 0:0:0');                $end=  strtotime(date('Y-m-d', strtotime("-1 day")).' 23:59:59');                $where['orderAddTime'] = ['between',[$starttime,$end]];            }else{                //今天                $starttime = strtotime(date('Y-m-d').' 0:0:0');                $end=  strtotime(date('Y-m-d').' 23:59:59');                $where['orderAddTime'] = ['between',[$starttime,$end]];            }        }        $list = Db::name('order_motion')            ->where($where)            ->order('orderAddTime desc')            ->paginate([                'list_rows'     => 10,                'page' => $page,            ])->toArray();        foreach ($list['data'] as &$value){            $user = Db::name('user')->where('userId', '=', $value['userId'])->find();            $value['user'] = $user;            $value['orderAddTime'] = date('Y-m-d',$value['orderAddTime']);        }        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' =>  $list        ));    }    /**     * 管理端 - 商场订单详情     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function getMotionDetail(){        $orderId = input('orderId', '');        $orderInfo = Db::name('order_motion')->where('orderMotionId', '=', $orderId)->find();        $orderInfo['user'] = Db::name('user')->where('userId', '=', $orderInfo['userId'])->find();        $orderInfo['orderAddTime'] = date('Y-m-d',$orderInfo['orderAddTime']);        $orderGoods = Db::name('order_motion_goods')->where('order_id', '=', $orderInfo['orderMotionId'])->select();        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' => array(                'orderInfo' =>$orderInfo,                'orderGoods' => $orderGoods,            ),        ));    }    /**     * 分段取消订单     * @return \think\response\Json     */    public function doCancel(){        $params = input();        $orderId = $params['order_id'];  //订单id        $timePeriodType = $params['type']; //时间段标识        if(empty($orderId)){            return json(array('code' => 500, 'msg' => '订单id不能为空'));        }        if(empty($timePeriodType)){            return json(array('code' => 500, 'msg' => '时间段标识不能为空'));        }        $timePeriodType = explode(',',$timePeriodType);        foreach ($timePeriodType as $v){            $this->doCancels($orderId,$v);        }        return json(array('code' => 200, 'msg' => '成功'));    }    public function doCancels($orderId,$timePeriodType){        $orderInfo = Db::name('order_subscribe')            ->where('orderSubscribeId', '=', $orderId)            ->find();        $userInfo = Db::name('user')->where('userId',$orderInfo['userId'])->find();        if ($orderInfo['orderStatus'] == '-1' || $orderInfo['orderStatus'] == '2') {            return json(array('code' => 500, 'msg' => '订单状态错误'));        }        $orderStartTime = config('timePeriod')[$timePeriodType-1];        $orderTimePeriodType = explode(',',$orderInfo['orderTimePeriodType']);        $min =  min($orderTimePeriodType);        $newOrderTimePeriodType = $orderInfo['orderTimePeriodTypeLog'];        $orderTimePeriodTypeAyy = explode(',',$newOrderTimePeriodType);        //判断订单是存在免费并且当前时间段就是免费时间段        if($orderInfo['freeAmount'] !=0  && $timePeriodType == $min){            //免费时间段            Db::startTrans();            try {                $catchBalance = array(                    'userId' => $userInfo['userId'],                    'balanceAmount' => 0,                    'addTime' => time(),                    'balanceNote' => '预约订单:' . $orderInfo['orderSubscribeNo'] . ',时间段:' . $orderStartTime['startTime'] . '-' . $orderStartTime['endTime'] . ",取消退款",                    'sourceNote' => '球场球位预约订单退款',                );                Db::name('user_balance_log')->insertGetId($catchBalance);                Db::name('user')->where('userId', '=', $userInfo['userId'])->setDec('userIntegral', 0);                $catchIntegral = array(                    'userId' => $userInfo['userId'],                    'integralAmount' => "-" . 0,                    'addTime' => time(),                    'integralNote' => '预约订单：' . $orderInfo['orderSubscribeNo'] . ',时间段:' . $orderStartTime['startTime'] . '-' . $orderStartTime['endTime'] . ",取消积分退回",                    'sourceNote' => '球场球位预约取消-退回积分',                );                Db::name('user_integral_log')->insertGetId($catchIntegral);                $order_subscribe_fen = array(                    'orderSubscribeId' => $orderId,                    'orderTimePeriodType' => $timePeriodType,                    'orderPayName' => '免费',                    'cancelTime' => time(),                    'orderRefundPrice' => 0,                );                $subscribe_fen = Db::name('order_subscribe_fen')->where(['orderSubscribeId' => $orderId, 'orderTimePeriodType' => $timePeriodType])->find();                if($subscribe_fen){                    return json(array('code' => 500, 'msg' => '订单已取消,操作失败'));                }                Db::name('order_subscribe_fen')->insert($order_subscribe_fen);                $memberTimeType = explode(',',$orderInfo['memberTimeType']);                if(in_array($timePeriodType,$memberTimeType)){                    $memberTimeType = array_diff($memberTimeType,[$timePeriodType]);                }                $count = Db::name('order_subscribe_fen')->where('orderSubscribeId',$orderId)->count();                if(count($orderTimePeriodTypeAyy) == $count){                    Db::name('order_subscribe')->where('orderSubscribeId', '=', $orderInfo['orderSubscribeId'])->save(array(                        'orderStatus' => '-1',                        'orderRefundPrice' => $orderInfo['orderRefundPrice'] + 0,                        'orderRefundNo' => 'YYTK' . date('Ymd', time()) . $userInfo['userId'] . rand(1000, 9999),                        'freeAmount' => 0,                        'orderTimePeriodType'=>$newOrderTimePeriodType,                        'memberTimeType'=>implode(',',$memberTimeType),                    ));                }else{                    $add = [];                    foreach($orderTimePeriodType as $v){                        if($v != $timePeriodType){                            $add[] = $v;                        }                    }                    Db::name('order_subscribe')->where('orderSubscribeId', '=', $orderInfo['orderSubscribeId'])->save(array(                        'orderTimePeriodType'=>implode(',',$add),                        'freeAmount'=>0,                        'memberTimeType'=>implode(',',$memberTimeType),                    ));                }                (new Payment())->unsubscribeSms($orderInfo,$orderStartTime);                Db::commit();                return json(array('code' => 200, 'msg' => '取消成功'));            } catch (\Exception $exception) {                Db::rollback();                return json(array('code' => 500, 'msg' => $exception->getMessage()));            }        }else{            if($orderInfo['orderPayName'] == '余额支付' || $orderInfo['orderPayName'] == 'balance'){                $positionPrice = (new Payment())->positionPrice($userInfo,$orderInfo,$timePeriodType);                Db::startTrans();                try {                    Db::name('user')->where('userId', '=', $userInfo['userId'])->setInc('userBalance', $positionPrice);                    $catchBalance = array(                        'userId' => $userInfo['userId'],                        'balanceAmount' => $positionPrice,                        'addTime' => time(),                        'balanceNote' => '预约订单:' . $orderInfo['orderSubscribeNo'] . ',时间段:' . $orderStartTime['startTime'] . '-' . $orderStartTime['endTime']. ",取消退款",                        'sourceNote' => '球场球位预约订单退款',                    );                    Db::name('user_balance_log')->insertGetId($catchBalance);                    $orderIntegralPrice = round($positionPrice * (new Payment())->integralConfig['integralConfig'], 2);                    Db::name('user')->where('userId', '=', $userInfo['userId'])->setDec('userIntegral', $orderIntegralPrice);                    //分销上级扣除积分                    $superior = new Payment();                    $superior->superiorIntegralDec($orderInfo['userId'],$orderIntegralPrice);                    $catchIntegral = array(                        'userId' => $userInfo['userId'],                        'integralAmount' => "-" . $orderIntegralPrice,                        'addTime' => time(),                        'integralNote' => '预约订单：' . $orderInfo['orderSubscribeNo']. ',时间段:' . $orderStartTime['startTime'] . '-' . $orderStartTime['endTime'] . ",取消积分退回",                        'sourceNote' => '球场球位预约取消-退回积分',                    );                    Db::name('user_integral_log')->insertGetId($catchIntegral);                    $order_subscribe_fen = array(                        'orderSubscribeId' => $orderId,                        'orderTimePeriodType' => $timePeriodType,                        'orderPayName' => '余额支付',                        'cancelTime' => time(),                        'orderRefundPrice' => $positionPrice,                    );                    $subscribe_fen = Db::name('order_subscribe_fen')->where(['orderSubscribeId' => $orderId, 'orderTimePeriodType' => $timePeriodType])->find();                    if($subscribe_fen){                        return json(array('code' => 500, 'msg' => '订单已取消,操作失败'));                    }                    Db::name('order_subscribe_fen')->insert($order_subscribe_fen);                    $memberTimeType = explode(',',$orderInfo['memberTimeType']);                    if(in_array($timePeriodType,$memberTimeType)){                        $memberTimeType = array_diff($memberTimeType,[$timePeriodType]);                    }                    $count = Db::name('order_subscribe_fen')->where('orderSubscribeId',$orderId)->count();                    if(count($orderTimePeriodTypeAyy) == $count) {                        Db::name('order_subscribe')->where('orderSubscribeId', '=', $orderInfo['orderSubscribeId'])->save(array(                            'orderStatus' => '-1',                            'orderRefundPrice' => $positionPrice + $orderInfo['orderRefundPrice'],                            'orderRefundNo' => 'YYTK' . date('Ymd', time()) . $userInfo['userId'] . rand(1000, 9999),                            'freeAmount' => 0,                            'orderTimePeriodType'=>$newOrderTimePeriodType,                            'memberTimeType'=>implode(',',$memberTimeType),                        ));                    }else{                        $add = [];                        foreach($orderTimePeriodType as $v){                            if($v != $timePeriodType){                                $add[] = $v;                            }                        }                        Db::name('order_subscribe')->where('orderSubscribeId', '=', $orderInfo['orderSubscribeId'])->save(array(                            'orderRefundPrice' => $positionPrice + $orderInfo['orderRefundPrice'],                            'orderTimePeriodType'=>implode(',',$add),                            'memberTimeType'=>implode(',',$memberTimeType),                        ));                    }                    (new Payment())->unsubscribeSms($orderInfo,$orderStartTime);                    Db::commit();                    return json(array('code' => 200, 'msg' => '取消成功'));                } catch (\Exception $exception) {                    Db::rollback();                    return json(array('code' => 500, 'msg' => $exception->getMessage()));                }            }elseif($orderInfo['orderPayName'] == '微信支付') {                $positionPrice = (new Payment())->positionPrice($userInfo,$orderInfo,$timePeriodType);                $orderInfo['orderRefundNo'] = 'YYTK' . date('Ymd', time()) . $userInfo['userId'] . rand(1000, 9999);                $orderInfo['orderRefundPrice'] = $positionPrice;                $orderInfo['orderActualPrice'] = $positionPrice;                $res = (new Payment())->payment->paymentRefund($orderInfo);                if ($res['return_code'] == "SUCCESS") {                    $orderIntegralPrice = round($positionPrice * (new Payment())->integralConfig['integralConfig'], 2);                    Db::name('user')->where('userId', '=', $orderInfo['userId'])->setDec('userIntegral', $orderIntegralPrice);                    //分销上级扣除积分                    $superior = new Payment();                    $superior->superiorIntegralDec($orderInfo['userId'],$orderIntegralPrice);                    $catchIntegral = array(                        'userId' => $orderInfo['userId'],                        'integralAmount' => "-" . $orderIntegralPrice,                        'addTime' => time(),                        'integralNote' => '预约订单：' . $orderInfo['orderSubscribeNo'] . ',时间段:' . $orderStartTime['startTime'] . '-' . $orderStartTime['endTime']. ",取消积分退回",                        'sourceNote' => '球场球位预约取消-退回积分',                    );                    Db::name('user_integral_log')->insertGetId($catchIntegral);                    $order_subscribe_fen = array(                        'orderSubscribeId' => $orderId,                        'orderTimePeriodType' => $timePeriodType,                        'orderPayName' => '微信支付',                        'cancelTime' => time(),                        'orderRefundPrice' => $res['refund_fee'] / 100,                        'orderRefundNo' => $res['out_refund_no'],                    );                    $subscribe_fen = Db::name('order_subscribe_fen')->where(['orderSubscribeId' => $orderId, 'orderTimePeriodType' => $timePeriodType])->find();                    if($subscribe_fen){                        return json(array('code' => 500, 'msg' => '订单已取消,操作失败'));                    }                    Db::name('order_subscribe_fen')->insert($order_subscribe_fen);                    $memberTimeType = explode(',',$orderInfo['memberTimeType']);                    if(in_array($timePeriodType,$memberTimeType)){                        $memberTimeType = array_diff($memberTimeType,[$timePeriodType]);                    }                    $count = Db::name('order_subscribe_fen')->where('orderSubscribeId',$orderId)->count();                    if(count($orderTimePeriodTypeAyy) == $count) {                        Db::name('order_subscribe')->where('orderSubscribeId', '=', $orderInfo['orderSubscribeId'])->save(array(                            'orderStatus' => '-1',                            'orderRefundPrice' => $positionPrice + ($res['refund_fee'] / 100),                            'orderRefundNo' => $res['out_refund_no'],                            'orderRefundId' => $res['refund_id'],                            'freeAmount' => 0,                            'orderTimePeriodType'=>$newOrderTimePeriodType,                            'memberTimeType'=>implode(',',$memberTimeType),                        ));                    }else{                        $add = [];                        foreach($orderTimePeriodType as $v){                            if($v != $timePeriodType){                                $add[] = $v;                            }                        }                        Db::name('order_subscribe')->where('orderSubscribeId', '=', $orderInfo['orderSubscribeId'])->save(array(                            'orderRefundPrice' => $positionPrice + ($res['refund_fee'] / 100),                            'orderTimePeriodType'=>implode(',',$add),                            'memberTimeType'=>implode(',',$memberTimeType),                        ));                    }                    (new Payment())->unsubscribeSms($orderInfo,$orderStartTime);                    return json(array('code' => 200, 'msg' => '取消成功'));                }else{                    return json(array('code' => 500, 'msg' => $res['return_msg']));                }            }        }    }    /**     * 用户抵扣券列表     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function deduction_list(){        $token = input('token', '');        $userInfo = $this->getUserInfo($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $data = Db::name('user_voucher')->alias('a')            ->join('voucher v','a.voucherId = v.voucherId','LEFT')            ->where('userId',$userInfo['userId'])            ->order('a.addTime','desc')            ->field('a.*,v.voucherName,v.voucherAmount,v.voucherType')            ->select();        foreach($data as $k=>$v){            $data[$k]['addTime'] = date('Y-m-d H:i',$v['addTime']);        }        return json(array(            'code' => 200,            'msg' => 'success:获取成功',            'data' => $data        ));    }    /**     * 赠送抵扣券     * @return \think\response\Json     * @throws Exception     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     * @throws \think\exception\PDOException     */    public function deduction_give(){        $token = input('token', '');        $userInfo = $this->getUserInfo($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $quilt_donor_mobile = input('quilt_donor_mobile', '');        $voucher_id = input('voucher_id', '');        if(empty($quilt_donor_mobile)){            return json(array('code' => 403, 'msg' => 'error:请输入被赠送人电话'));        }        if(empty($voucher_id)){            return json(array('code' => 403, 'msg' => 'error:抵扣券id不能为空'));        }        $user_voucher = Db::name('user_voucher')->where(['userVoucherId'=>$voucher_id,'userId'=>$userInfo['userId']])->find();        if(empty($user_voucher)){            return json(array('code' => 403, 'msg' => 'error:该抵扣券不存在'));        }        if($user_voucher['states'] == 1){            return json(array('code' => 403, 'msg' => 'error:该抵扣券已被使用'));        }        $donor = Db::name('user')->where(['userMobile'=>$quilt_donor_mobile,'userStatus'=>1])->find();        if(empty($donor)){            return json(array('code' => 403, 'msg' => 'error:没找到该被赠送人'));        }        $data = Db::name('user_voucher')->where(['userVoucherId'=>$user_voucher['userVoucherId']])->update(['userId'=>$donor['userId'],'donor_uid'=>$userInfo['userId']]);        if($data){            $SmsLogic = new SmsLogic();            $content = "尊敬的球友，您好。您的好友给您赠送了1张抵扣券，请前往小程序使用（微信小程序搜索“T-GOLF”）。祝您开心每一天：）";            $newMsg = urlencode($content);            $SmsRes = $SmsLogic->realSendSms($quilt_donor_mobile, tpCache('sms.sms_appkey'),tpCache('sms.sms_psd'), $newMsg);            return json(array('code' => 200, 'msg' => 'success:赠送成功'));        }else{            return json(array('code' => 403, 'msg' => 'error:赠送失败'));        }    }    public function getOrderCount($table, $orderStatus){        $where = array();        if ($orderStatus != '') {            $where['orderStatus'] = array('=', $orderStatus);        }        $count = Db::name($table)            ->where($where)            ->count();        return $count;    }    /**     * 获取订单数量     * @return \think\response\Json     */    public function getUserOrderConfig(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $orderType = input('orderType', '');        if (empty($orderType)) {            return json(array('code' => 500, 'msg' => '缺少参数orderType'));        }        if ($orderType == 'subscribe') {            return json(array(                'code' => 200,                'msg' => 'success:获取成功',                'data' => array(                    'list' => array(                        array('orderStatusName' => '全部', 'orderStatus' => '', 'orderCount' => $this->getOrderCount('order_subscribe', '')),                        array('orderStatusName' => '已预约', 'orderStatus' => '1', 'orderCount' => $this->getOrderCount('order_subscribe', '1')),                        array('orderStatusName' => '待付款', 'orderStatus' => '0', 'orderCount' => $this->getOrderCount('order_subscribe', '0')),                        array('orderStatusName' => '已取消', 'orderStatus' => '-1', 'orderCount' => $this->getOrderCount('order_subscribe', '-1')),                        array('orderStatusName' => '已完成', 'orderStatus' => '2', 'orderCount' => $this->getOrderCount('order_subscribe', '2')),                    ),                    'orderType' => $orderType                ),            ));        }elseif ($orderType == 'course') {            return json(array(                'code' => 200,                'msg' => 'success:获取成功',                'data' => array(                    'list' => array(                        array('orderStatusName' => '全部', 'orderStatus' => '', 'orderCount' => $this->getOrderCount('order_course', '')),                        array('orderStatusName' => '未使用', 'orderStatus' => '1', 'orderCount' => $this->getOrderCount('order_course', '1')),                        array('orderStatusName' => '进行中', 'orderStatus' => '3', 'orderCount' => $this->getOrderCount('order_course', '3')),                        array('orderStatusName' => '已完成', 'orderStatus' => '2', 'orderCount' => $this->getOrderCount('order_course', '2')),                        array('orderStatusName' => '待付款', 'orderStatus' => '0', 'orderCount' => $this->getOrderCount('order_course', '0')),                    ),                    'orderType' => $orderType                ),            ));        }elseif ($orderType == 'motion') {            return json(array(                'code' => 200,                'msg' => 'success:获取成功',                'data' => array(                    'list' => array(                        array('orderStatusName' => '全部', 'orderStatus' => '', 'orderCount' => $this->getOrderCount('order_motion', '')),                        array('orderStatusName' => '待自取', 'orderStatus' => '1', 'orderCount' => $this->getOrderCount('order_motion', '1')),                        array('orderStatusName' => '已完成', 'orderStatus' => '2', 'orderCount' => $this->getOrderCount('order_motion', '2')),                        array('orderStatusName' => '待付款', 'orderStatus' => '0', 'orderCount' => $this->getOrderCount('order_motion', '0')),                    ),                    'orderType' => $orderType                ),            ));        }else{            return json(array(                'code' => 500,                'msg' => 'error:未获取到正确的orderType参数'            ));        }    }    /**     * 用户列表     * @return \think\response\Json     * @throws \think\exception\DbException     */    public function user_list(){        $token = input('token', '');        $page = input('page', '');        $keyword = input('keyword', '');        $gradeId= input('gradeId', '');        $orderUser= input('orderUser', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $where = [];        if(isset($gradeId) && $gradeId != ''){            $where['gradeId'] = $gradeId;        }        if($userInfo['role'] == 2){            $where['sale_id'] = $userInfo['user_admin_id'];        }        if($orderUser == 1){            //当前有球位订单用户            if($keyword){                $where['u.nickName|u.userMobile'] = ['like','%'.$keyword.'%'];            }            $where['a.orderStatus'] = 1;            $where['a.orderIsDel'] = 0;            $data = Db::name('order_subscribe')->alias('a')                ->join('user u','a.userId = u.userId','LEFT')                ->join('user_admin c','c.user_admin_id = u.sale_id','LEFT')                ->where($where)                ->order('orderAddTime','desc')                ->group('a.userId')                ->field('u.*,a.orderSubscribeId,c.nickname as sale_name')                ->paginate([                    'list_rows'     => 10,                    'page' => $page,                ]);        }elseif($orderUser == 2){            //当前有课程订单用户            if($keyword){                $where['u.nickName|u.userMobile'] = ['like','%'.$keyword.'%'];            }            $data = Db::name('user_make_course')->alias('a')                ->join('order_coach_subscribe o','a.userMakeCourseId = o.userMakeCourseId','LEFT')                ->join('user u','a.userId = u.userId','LEFT')                ->join('user_admin c','c.user_admin_id = u.sale_id','LEFT')                ->where('a.makeCourseStatus','in',[1,4])                ->order('orderAddTime','desc')                ->group('a.userId')                ->field('u.*,a.userMakeCourseId,c.nickname as sale_name')                ->paginate([                    'list_rows'     => 10,                    'page' => $page,                ]);        }else{            if($keyword){                $where['a.nickName|a.userMobile'] = ['like','%'.$keyword.'%'];            }            $data = Db::name('user')->alias('a')                ->join('user_admin c','c.user_admin_id = a.sale_id','LEFT')                ->field('a.*,c.nickname as sale_name')                ->where($where)->order('addTime','desc')->paginate([                'list_rows'     => 10,                'page' => $page,            ]);        }        return json(array('code' => 200, 'msg' => 'success:成功','data'=>$data));    }    /**     * 用户详情     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function user_details(){        $userId = input('userId', '');        if (empty($userId)) {            return json(array('code' => 403, 'msg' => 'error:参数错误'));        }        $data = Db::name('user')->where('userId',$userId)->find();        if($data){            $data['addTime'] = $data['addTime']?date('Y年m月d日',$data['addTime']):'';            $data['order_subscribe'] = Db::name('order_subscribe')->where(['userId'=>$userId,'orderStatus'=>1,'orderIsDel'=>0])->field('orderSubscribeId,orderStatus,orderTimePeriodType,orderTimePeriodTypeLog,orderPositionName,orderWeek,orderDate')->select();            foreach($data['order_subscribe'] as $k=>$v){                $data['order_subscribe'][$k]['orderDate'] = $v['orderDate']?date('Y年m月d日',$v['orderDate']):'';                if($v['orderTimePeriodType']){                    $timePeriodType = explode(',', $v['orderTimePeriodType']);                    $orderTimePeriodType = [];                    foreach($timePeriodType as $m=>$n){                        $orderTimePeriodType[] = config('timePeriod')[$n-1]['startTime'].'-'.config('timePeriod')[$n-1]['endTime'];                    }                    $data['order_subscribe'][$k]['orderTimePeriodType'] = $orderTimePeriodType;                }            }            $data['course_subscribe'] = Db::name('user_make_course')->alias('a')                ->join('order_coach_subscribe o','a.userMakeCourseId=o.userMakeCourseId','LEFT')                ->where(['a.userId'=>$userId,'makeCourseStatus'=>1])                ->field('a.userMakeCourseId,o.orderCoachSubscribeId,o.orderStatus,o.orderTimePeriodType,o.orderPositionName,o.orderWeek,o.orderDate')                ->select();            foreach($data['course_subscribe'] as $k1=>$v1){                $data['course_subscribe'][$k1]['orderDate'] = $v1['orderDate']?date('Y年m月d日',$v1['orderDate']):'';                if($v1['orderTimePeriodType']){                    $timePeriodTypes = explode(',', $v1['orderTimePeriodType']);                    $orderTimePeriodTypes = [];                    foreach($timePeriodTypes as $m1=>$n1){                        $orderTimePeriodTypes[] = config('timePeriod')[$n1-1]['startTime'].'-'.config('timePeriod')[$n1-1]['endTime'];                    }                    $data['course_subscribe'][$k1]['orderTimePeriodType'] = $orderTimePeriodTypes;                }            }        }        return json(array('code' => 200, 'msg' => 'success:成功','data'=>$data));    }    /**     * 用户卡包     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function card_holder(){        $userId = input('userId', '');        $holder= input('holder', '');        $holder_type= input('holder_type', '');        $page= input('page', '');        if (empty($userId) || empty($holder)) {            return json(array('code' => 403, 'msg' => 'error:参数错误'));        }        $holder_type = $holder_type==''?'0':$holder_type;        if($holder == 1){            //优惠券            $data = Db::name('user_coupon')->alias('a')                ->join('coupon c','a.couponId = c.couponId','LEFT')                ->where(['a.userId'=>$userId,'a.couponStatus'=>$holder_type])                ->order('a.addTime','desc')                ->field('a.*,c.couponName,c.couponAmount')                ->paginate([                    'list_rows'     => 10,                    'page' => $page,                ]);        }elseif($holder == 2){            //年卡抵用券            $data = Db::name('user_voucher')->alias('a')                ->join('voucher v','a.voucherId = v.voucherId','LEFT')                ->where(['a.userId'=>$userId,'a.status'=>$holder_type,'v.voucherType'=>2])                ->order('a.addTime','desc')                ->field('a.*,v.voucherName,v.voucherAmount')                ->paginate([                    'list_rows'     => 10,                    'page' => $page,                ]);        }else{            //充值卡            if($holder_type == 1){                $holder_type = 2;            }else{                $holder_type = 1;            }            $data = Db::name('rechargeable')                ->where(['uid'=>$userId,'states'=>$holder_type,'types'=>1,'rechargeableIsDel'=>0])                ->order('createtime','desc')                ->field('id,card_number,card_password,denomination')                ->paginate([                    'list_rows'     => 10,                    'page' => $page,                ]);        }        return json(array('code' => 200, 'msg' => 'success:成功','data'=>$data));    }    /**     * 卡券数量     * @return \think\response\Json     */    public function coupons_num(){        $userId = input('userId', '');        if (empty($userId)) {            return json(array('code' => 403, 'msg' => 'error:参数错误'));        }        //优惠券        $user_coupon = Db::name('user_coupon')->where(['userId'=>$userId,'couponStatus'=>0])->count();        //年卡抵用券        $user_voucher = Db::name('user_voucher')->alias('a')            ->join('voucher v','a.voucherId = v.voucherId','LEFT')            ->where(['a.userId'=>$userId,'a.status'=>0,'v.voucherType'=>2])            ->count();        //充值卡        $rechargeable = Db::name('rechargeable')->where(['uid'=>$userId,'states'=>1,'types'=>1,'rechargeableIsDel'=>0])->count();        $num = $user_coupon + $user_voucher + $rechargeable;        return json(array('code' => 200, 'msg' => 'success:成功','data'=>$num));    }    /**     * 值班经理 - 增减余额     * @return \think\response\Json     * @throws Exception     */    public function doUserBalanceAdd(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if($userInfo['role'] != 1){            return json(array('code' => 403, 'msg' => 'error:您还不是值班经理'));        }        $params = input('post.');        if (empty($params['amount'])) {            return json(array('code' => 500, 'msg' => '请输入金额'));        }        if(empty($params['type']) || empty($params['userId'])){            return json(array('code' => 500, 'msg' => '参数错误'));        }        $user = Db::name('user')->where('userId',$params['userId'])->field('nickName,userBalance')->find();        if($params['type'] == 1){            //增加            $amount = $params['amount'];            $way = '增加';        }else{            //扣除            if($params['amount'] > $user['userBalance']){                return json(array('code' => 500, 'msg' => '扣除的金额不能大于用户余额'));            }            $amount = $params['amount']*(-1);            $way = '扣除';        }        $catch = array(            'userId' => $userInfo['user_admin_id'],            'quUserId' => $params['userId'],            'amount' => $amount,            'addTime' => time(),            'note' => $params['note'],            'behavior' => $userInfo['nickname'].$way.$user['nickName'].$params['amount'].'元余额',        );        $data = Db::name('user')->where('userId', '=', $params['userId'])->setInc('userBalance', $amount);        if($data){           Db::name('operate_log')->insertGetId($catch);        }        return json(array('code' => 200, 'msg' => '操作成功'));    }    /**     * 值班经理-增减积分     * @return \think\response\Json     * @throws Exception     */    public function doUserIntegralAdd(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if($userInfo['role'] != 1){            return json(array('code' => 403, 'msg' => 'error:您还不是值班经理'));        }        $params = input('post.');        if (empty($params['amount'])) {            return json(array('code' => 500, 'msg' => '请输入积分'));        }        if(empty($params['type']) || empty($params['userId'])){            return json(array('code' => 500, 'msg' => '参数错误'));        }        $user = Db::name('user')->where('userId',$params['userId'])->field('nickName,userIntegral')->find();        if($params['type'] == 1){            //增加            $amount = $params['amount'];            $way = '增加';        }else{            //扣除            if($params['amount'] > $user['userIntegral']){                return json(array('code' => 500, 'msg' => '扣除的积分不能大于用户积分'));            }            $amount = $params['amount']*(-1);            $way = '扣除';        }        $catch = array(            'userId' => $userInfo['user_admin_id'],            'quUserId' => $params['userId'],            'amount' => $amount,            'addTime' => time(),            'note' => $params['note'],            'behavior' => $userInfo['nickname'].$way.$user['nickName'].$params['amount'].'积分',        );        $data = Db::name('user')->where('userId', '=', $params['userId'])->setInc('userIntegral', $amount);        if($data){            Db::name('operate_log')->insertGetId($catch);        }        return json(array('code' => 200, 'msg' => '操作成功'));    }    /**     * 卡券分类     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function coupons_category(){        //优惠券        $coupon = Db::name('coupon')->where('couponIsDel', '=', '0')->select();        //抵扣券        $voucher = Db::name('voucher')->where('voucherType', '=', 2)->select();        //充值卡        $card = Db::name('card_set')->select();        $data = [            'coupon' => $coupon,            'voucher' => $voucher,            'card' => $card,        ];        return json(array('code' => 200, 'msg' => '操作成功','data'=>$data));    }    /**     * 值班经理-赠送卡券     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function doCouponsAdd(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if($userInfo['role'] != 1){            return json(array('code' => 403, 'msg' => 'error:您还不是值班经理'));        }        $params = input('post.');        if(empty($params['type']) || empty($params['userId'])){            return json(array('code' => 500, 'msg' => '参数错误'));        }        if (empty($params['couponsId'])) {            return json(array('code' => 500, 'msg' => '请选择发放的卡券'));        }        if($params['type'] == 1){            //优惠券            $coupon = Db::name('coupon')->where('couponId', '=', $params['couponsId'])->find();            if (empty($coupon)) {                return json(array('code' => 500, 'msg' => '优惠券数据出错，请联系管理员处理'));            }            $nickName = Db::name('user')->where('userId',$params['userId'])->value('nickName');            $endTime = 0;            if ($coupon['validTime'] > 0) {                $endTime = strtotime(date("Y-m-d",strtotime("+".$coupon['validTime']." day")));            }            $add = array(                'userId' => $userInfo['user_admin_id'],                'quUserId' => $params['userId'],                'addTime' => time(),                'note' => $params['note'],                'behavior' => $userInfo['nickname'].'赠送'.$nickName.$coupon['couponName'].$coupon['couponAmount'].'一张',            );            $catch = array(                'couponId' => $coupon['couponId'],                'userId' => $params['userId'],                'addTime' => strtotime(date('Y-m-d', time())),                'endTime' => $endTime,            );            $data = Db::name('user_coupon')->insertGetId($catch);            if($data){                Db::name('operate_log')->insertGetId($add);            }            return json(array('code' => 200, 'msg' => '发放成功'));        }elseif($params['type'] == 2){            //抵扣券            $voucher = Db::name('voucher')->where('voucherId', '=', $params['couponsId'])->find();            if (empty($voucher)) {                return json(array('code' => 500, 'msg' => '抵扣券数据出错，请联系管理员处理'));            }            $nickName = Db::name('user')->where('userId',$params['userId'])->value('nickName');            $add = array(                'userId' => $userInfo['user_admin_id'],                'quUserId' => $params['userId'],                'addTime' => time(),                'note' => $params['note'],                'behavior' => $userInfo['nickname'].'赠送'.$nickName.$voucher['voucherName'].'一张',            );            $catch = array(                'voucherId' => $voucher['voucherId'],                'userId' => $params['userId'],                'addTime' => strtotime(date('Y-m-d', time())),            );            $data = Db::name('user_voucher')->insertGetId($catch);            if($data){                Db::name('operate_log')->insertGetId($add);            }            return json(array('code' => 200, 'msg' => '发放成功'));        }else{            //充值卡            $card_set = Db::name('card_set')->where('id', '=', $params['couponsId'])->find();            if (empty($card_set)) {                return json(array('code' => 500, 'msg' => '充值卡数据出错，请联系管理员处理'));            }            $user = Db::name('user')->where('userId',$params['userId'])->field('nickName,userMobile')->find();            $add = array(                'userId' => $userInfo['user_admin_id'],                'quUserId' => $params['userId'],                'addTime' => time(),                'note' => $params['note'],                'behavior' => $userInfo['nickname'].'赠送'.$user['nickName'].'虚拟充值卡'.$card_set['amount'].'一张',            );            $catch = array(                'denomination' => $card_set['amount'],                'number' => 1,                'userMobile'=>$user['userMobile'],                'types'=>1,                'substantialCharge'=>$card_set['reality_amount'],            );            $model = new \app\mingteng\model\Rechargeable();            $data = $model->virtually($catch,$params);            if($data){                Db::name('operate_log')->insertGetId($add);            }            return json(array('code' => 200, 'msg' => '发放成功'));        }    }    /**     * 删除卡券     * @return \think\response\Json     * @throws Exception     * @throws \think\exception\PDOException     */    public function delCoupons(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $params = input('post.');        if(empty($params['type']) || empty($params['userId'])){            return json(array('code' => 500, 'msg' => '参数错误'));        }        if (empty($params['userCouponsId'])) {            return json(array('code' => 500, 'msg' => '请选择需要删除的卡券id'));        }        $nickName = Db::name('user')->where('userId',$params['userId'])->value('nickName');        if($params['type'] == 1){            //优惠券            $coupon = Db::name('user_coupon')->alias('a')                ->join('coupon c','a.couponId = c.couponId','LEFT')                ->where('a.userCouponId',$params['userCouponsId'])                ->field('a.*,c.couponName,c.couponAmount')                ->find();            if (empty($coupon)) {                return json(array('code' => 500, 'msg' => '优惠券数据出错，请联系管理员处理'));            }            $add = array(                'userId' => $userInfo['user_admin_id'],                'quUserId' => $params['userId'],                'addTime' => time(),                'note' => $params['note'],                'behavior' => $userInfo['nickname'].'删除'.$nickName.$coupon['couponName'].$coupon['couponAmount'].'元1张',            );            $data = Db::name('user_coupon')->where('userCouponId',$params['userCouponsId'])->delete();            if($data){                Db::name('operate_log')->insertGetId($add);            }        }elseif($params['type'] == 2){            //抵扣券            $coupon = Db::name('user_voucher')->alias('a')                ->join('voucher v','a.voucherId = v.voucherId','LEFT')                ->where('a.userVoucherId',$params['userCouponsId'])                ->field('a.*,v.voucherName,v.voucherAmount')                ->find();            if (empty($coupon)) {                return json(array('code' => 500, 'msg' => '抵扣券数据出错，请联系管理员处理'));            }            $add = array(                'userId' => $userInfo['user_admin_id'],                'quUserId' => $params['userId'],                'addTime' => time(),                'note' => $params['note'],                'behavior' => $userInfo['nickname'].'删除'.$nickName.$coupon['voucherName'].'1张',            );            $data = Db::name('user_voucher')->where('userVoucherId',$params['userCouponsId'])->delete();            if($data){                Db::name('operate_log')->insertGetId($add);            }        }else{            //充值卡            $coupon = Db::name('rechargeable')->where('id',$params['userCouponsId'])->find();            if (empty($coupon)) {                return json(array('code' => 500, 'msg' => '充值卡数据出错，请联系管理员处理'));            }            $add = array(                'userId' => $userInfo['user_admin_id'],                'quUserId' => $params['userId'],                'addTime' => time(),                'note' => $params['note'],                'behavior' => $userInfo['nickname'].'删除'.$nickName.'虚拟充值卡'.$coupon['denomination'].'元1张',            );            $data = Db::name('rechargeable')->where('id',$params['userCouponsId'])->update(['rechargeableIsDel'=>1]);            if($data){                Db::name('operate_log')->insertGetId($add);            }        }        if($data){            return json(array('code' => 200, 'msg' => '删除成功'));        }else{            return json(array('code' => 500, 'msg' => '删除失败'));        }    }    /**     * 操作记录     * @return \think\response\Json     * @throws \think\exception\DbException     */    public function operate_log_list(){        $token = input('token', '');        $page = input('page', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $where = [];        if($userInfo['role'] == 2){            $where['userId'] = $userInfo['user_admin_id'];        }        $data = Db::name('operate_log')->alias('a')            ->join('user_admin u','a.userId = u.user_admin_id','LEFT')            ->where($where)            ->order('addTime','desc')            ->field('a.*,u.nickname')            ->paginate([            'list_rows'=> 10,            'page' => $page,        ])->toArray();        foreach ($data['data'] as $k=>$v){            $data['data'][$k]['addTime'] = date('Y年m月d日H:i',$v['addTime']);        }        return json(array('code' => 200, 'msg' => '成功','data'=>$data));    }    /**     * 修改该用户信息     * @return \think\response\Json     * @throws Exception     * @throws \think\exception\PDOException     */    public function user_info(){        $param = input();        if(empty($param['userId'])){            return json(array('code' => 403, 'msg' => 'error:用户id不能为空'));        }        $data = Db::name('user')->where(['userId'=>$param['userId']])->update([            'nickName' => $param['nickName'],            'userMobile' => $param['userMobile'],            'userSex' => $param['userSex'],            'birthdayYear' => $param['birthdayYear'],            'birthdayData' => $param['birthdayData'],            'sale_id' => $param['sale_id'],        ]);        return json(array('code' => 200, 'msg' => '成功','data'=>$data));    }    /**     * 销售顾问列表     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function sale_list(){        $data=  Db::name('user_admin')->where(['role'=>2,'status'=>0])->field('user_admin_id,user_name,nickname')->select();        return json(array('code' => 200, 'msg' => '成功','data'=>$data));    }    /**     * 该用户的操作记录     * @return \think\response\Json     * @throws \think\exception\DbException     */    public function user_operate_log_list(){        $userId= input('userId', '');        $page= input('page', '');        if(empty($userId)){            return json(array('code' => 403, 'msg' => 'error:用户id不能为空'));        }        $data = Db::name('operate_log')->alias('a')            ->join('user_admin s','a.userId = s.user_admin_id','LEFT')            ->where(['a.quUserId'=>$userId])            ->field('a.*,s.nickname')            ->paginate([            'list_rows'=> 10,            'page' => $page,        ])->toArray();        return json(array('code' => 200, 'msg' => '成功','data'=>$data));    }    /**     * 生成销售分享二维码     * @return false|string     */    public function neibu(){        $token = input('token', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $url = input('url');        $config  = new Config();        //生成团二维码        //scene是参数，为字符串。比如要传入一个用户id=1234，        //要根据这个用户id来给当前页面返回不同的内容，那么scene参数就可以写成"1234"，        //多个参数按一定规则分开，如&符号，第二个参数是recommendId=123则可以这样写"1234&123"。//        $chauncan = 'user_admin_id='.$userInfo['user_admin_id'];        $chauncan = $userInfo['user_admin_id'];        $admin_ewm = $config->get_unlimited($chauncan,$url);        $return['status'] = 'success';        $return['msg'] = '生成销售内部二维码成功';        $return['ewm'] =$admin_ewm;        return json_encode($return);    }    /**     * 绑定销售     * @return \think\response\Json     * @throws Exception     * @throws \think\exception\PDOException     */    public function binding_sale(){        $token = input('token', '');        $userInfo = $this->getUserInfo($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $sale_id = input('sale_id', '');        if(empty($sale_id)){            return json(array('code' => 403, 'msg' => 'error:销售id不能为空'));        }        $data=[];        $user_admin = Db::name('user_admin')->where('user_admin_id',$sale_id)->find();        if(empty($user_admin)){            return json(array('code' => 403, 'msg' => 'error:该销售不存在'));        }        $user = Db::name('user')->where('userId',$userInfo['userId'])->value('sale_id');        if(empty($user)){            $data = Db::name('user')->where('userId',$userInfo['userId'])->update(['sale_id'=>$sale_id]);        }else{            return json(array('code' => 403, 'msg' => 'error:您已经绑定了销售'));        }        return json(array(            'code' => 200,            'msg' => 'success:成功',            'data' => $data        ));    }    /**     * 待注意事项列表     * @return \think\response\Json     * @throws \think\exception\DbException     */    public function notes_list(){        $token = input('token', '');        $page = input('page', '');        $read_type = input('read_type', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $where=[];        if($read_type){            $where['read_type'] = $read_type;        }        if($userInfo['role'] == 1){            $data = Db::name('notes_to_taken')->where($where)->paginate([                'list_rows'=> 10,                'page' => $page,            ])->toArray();        }else{            $data = Db::name('notes_to_taken')->where('sale_id',$userInfo['user_admin_id'])->where($where)->paginate([                'list_rows'=> 10,                'page' => $page,            ])->toArray();        }        foreach($data['data'] as $k=>$v){            $data['data'][$k]['addTime'] = date('Y-m-d',$v['addTime']);        }        return json(array(            'code' => 200,            'msg' => 'success:成功',            'data' => $data        ));    }    /**     * 月度/年度会员到期提醒     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function notesMemberAdd(){        //月度/年度会员到期提醒        $user = Db::name('user')->where('gradeId','in',[1,2])->where('userStatus',1)->select();        $all = [];        foreach($user as $v){            if($v['gradeId'] == 1){                $time = $v['gradeEndTime'] - (60*60*24*7);                $notes_to_taken = Db::name('notes_to_taken')->where(['userId'=>$v['userId'],'event'=>'月度会员到期提醒'])->order('addTime','desc')->find();                $list = [                    'userId' => $v['userId'],                    'nickName' => $v['nickName'],                    'userMobile' => $v['userMobile'],                    'sale_id' => $v['sale_id'],                    'event' => '月度会员到期提醒',                    'addTime' => time(),                ];                if(time()>=$time && time() < $v['gradeEndTime']){                    if($notes_to_taken){                        if(time() > $notes_to_taken['addTime'] + (60*60*24*7)){                            $all[] = $list;                        }                    }else{                        $all[] = $list;                    }                }            }else{                $time = $v['gradeEndTime'] - (60*60*24*30);                $notes_to_taken = Db::name('notes_to_taken')->where(['userId'=>$v['userId'],'event'=>'年度会员到期提醒'])->order('addTime','desc')->find();                $list = [                    'userId' => $v['userId'],                    'nickName' => $v['nickName'],                    'userMobile' => $v['userMobile'],                    'sale_id' => $v['sale_id'],                    'event' => '年度会员到期提醒',                    'addTime' => time(),                ];                if(time()>=$time && time() < $v['gradeEndTime']){                    if($notes_to_taken){                        if(time() > $notes_to_taken['addTime'] + (60*60*24*30)){                            $all[] = $list;                        }                    }else{                        $all[] = $list;                    }                }            }        }        $add = Db::name('notes_to_taken')->insertAll($all);        return json(array(            'code' => 200,            'msg' => 'success:成功',            'data' => $add        ));    }    /**     * 用户生日提醒     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function notesBirthdayAdd(){        $user = Db::name('user')->where('userStatus',1)->where('birthdayData','<>','')->select();        $all=[];        foreach($user as $v){            $birthday = strtotime(date("Y").'-'.$v['birthdayData']);            $birthdayTq = $birthday - (60*60*24);            $notes_to_taken = Db::name('notes_to_taken')->where(['userId'=>$v['userId'],'event'=>'生日提醒'])->order('addTime','desc')->find();            $list = [                'userId' => $v['userId'],                'nickName' => $v['nickName'],                'userMobile' => $v['userMobile'],                'sale_id' => $v['sale_id'],                'event' => '生日提醒',                'addTime' => time(),            ];            if(time()>=$birthdayTq && time() < $birthday){                if($notes_to_taken){                    if(time() > $notes_to_taken['addTime'] + (60*60*24)){                        $all[] = $list;                    }                }else{                    $all[] = $list;                }            }        }        $add = Db::name('notes_to_taken')->insertAll($all);        return json(array(            'code' => 200,            'msg' => 'success:成功',            'data' => $add        ));    }    /**     * 已读     * @return \think\response\Json     * @throws Exception     * @throws \think\exception\PDOException     */    public function read(){        $token = input('token', '');        $id = input('id', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if(empty($id)){            return json(array('code' => 403, 'msg' => 'error:id不能为空'));        }        $data = Db::name('notes_to_taken')->where('id',$id)->update(['read_type' => 2]);        return json(array(            'code' => 200,            'msg' => 'success:成功',            'data' => $data        ));    }    /**     * 销售提交申请     * @return \think\response\Json     */    public function sale_apply_add(){        $token = input('token', '');        $post = input();        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if($userInfo['role'] != 2){            return json(array('code' => 403, 'msg' => 'error:你还不是销售'));        }        if(empty($post['type'])){            return json(array('code' => 403, 'msg' => 'error:type不能为空'));        }        if(empty($post['status'])){            return json(array('code' => 403, 'msg' => 'error:status不能为空'));        }        if(empty($post['userId'])){            return json(array('code' => 403, 'msg' => 'error:用户id不能为空'));        }        $user = Db::name('user')->where('userId',$post['userId'])->field('nickName,userBalance,userIntegral')->find();        if($post['type'] == 1){            //增减余额            if($post['status'] == 1){                $amount = $post['amount'];                $way = '增加';            }else{                if($post['amount'] > $user['userBalance']){                    return json(array('code' => 500, 'msg' => '扣除的余额不能大于用户余额'));                }                $amount = $post['amount']*(-1);                $way = '扣除';            }            $catch['amount'] = $amount;            $catch['behavior'] = $userInfo['nickname'].'申请'.$way.$user['nickName'].$post['amount'].'余额';        }elseif($post['type'] == 2){            //增减积分            if($post['status'] == 1){                $amount = $post['amount'];                $way = '增加';            }else{                if($post['amount'] > $user['userIntegral']){                    return json(array('code' => 500, 'msg' => '扣除的积分不能大于用户积分'));                }                $amount = $post['amount']*(-1);                $way = '扣除';            }            $catch['amount'] = $amount;            $catch['behavior'] = $userInfo['nickname'].'申请'.$way.$user['nickName'].$post['amount'].'积分';        }elseif($post['type'] == 3){            //赠送优惠券            $coupon = Db::name('coupon')->where('couponId', '=', $post['coupons_id'])->find();            if (empty($coupon)) {                return json(array('code' => 500, 'msg' => '优惠券数据出错，请联系管理员处理'));            }            $catch['behavior'] = $userInfo['nickname'].'申请赠送'.$user['nickName'].$coupon['couponName'].$coupon['couponAmount'].'一张';        }elseif($post['type'] == 4){            //赠送抵扣券            $voucher = Db::name('voucher')->where('voucherId', '=', $post['coupons_id'])->find();            if (empty($voucher)) {                return json(array('code' => 500, 'msg' => '抵扣券数据出错，请联系管理员处理'));            }            $catch['behavior'] = $userInfo['nickname'].'申请赠送'.$user['nickName'].$voucher['voucherName'].'一张';        }else{            //赠送充值卡            $card_set = Db::name('card_set')->where('id', '=', $post['coupons_id'])->find();            if (empty($card_set)) {                return json(array('code' => 500, 'msg' => '充值卡数据出错，请联系管理员处理'));            }            $catch['behavior'] = $userInfo['nickname'].'申请赠送'.$user['nickName'].'虚拟充值卡'.$card_set['amount'].'一张';        }        $post['user_admin_id']= $userInfo['user_admin_id'];        $post['apply_time'] = time();        $data = Db::name('sale_apply_list')->insert($post);        if($data){            $catch = array(                'userId' => $userInfo['user_admin_id'],                'quUserId' => $post['userId'],                'addTime' => time(),                'note' => $post['notes'],                'amount' => $catch['amount'],                'behavior' => $catch['behavior'],            );            Db::name('operate_log')->insertGetId($catch);        }        return json(array(            'code' => 200,            'msg' => 'success:成功',            'data' => $data        ));    }    /**     * 值班经理-销售申请列表     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function sale_apply_list(){        $token = input('token', '');        $examine_type = input('examine_type', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        $where =[];        if($examine_type != ''){            $where['a.examine_type'] = $examine_type;        }        $data = Db::name('sale_apply_list')->alias('a')            ->join('user u','a.userId = u.userId','LEFT')            ->join('user_admin x','a.user_admin_id = x.user_admin_id','LEFT')            ->where($where)            ->order('a.apply_time','desc')            ->field('a.*,u.nickName as u_name,x.nickname as x_name')            ->select();        if($data){            foreach($data as $k=>$v){                $data[$k]['apply_time'] = $v['apply_time']?date('Y-m-d',$v['apply_time']):'';                $data[$k]['examine_time'] = $v['examine_time']?date('Y-m-d',$v['examine_time']):'';                if($v['type'] == 3){                    $data[$k]['coupons_name'] = Db::name('coupon')->where('couponId',$v['coupons_id'])->value('couponName');                }elseif($v['type'] == 4){                    $data[$k]['coupons_name'] = Db::name('voucher')->where('voucherId',$v['coupons_id'])->value('voucherName');                }elseif($v['type'] == 5){                    $data[$k]['coupons_name'] = Db::name('rechargeable')->where('id',$v['coupons_id'])->value('card_number');                }            }        }        return json(array(            'code' => 200,            'msg' => 'success:成功',            'data' => $data        ));    }    /**     * 值班经理审核     * @return \think\response\Json     * @throws Exception     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     * @throws \think\exception\PDOException     */    public function examine(){        $token = input('token', '');        $sale_apply_id = input('sale_apply_id', '');        $examine_type = input('examine_type', '');        $refuse_reason = input('refuse_reason', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if($userInfo['role'] != 1){            return json(array('code' => 403, 'msg' => 'error:你还不是经理'));        }        if(empty($sale_apply_id)){            return json(array('code' => 403, 'msg' => 'error:id不能为空'));        }        if(empty($examine_type)){            return json(array('code' => 403, 'msg' => 'error:审核状态不能为空'));        }        $sale_apply_list = Db::name('sale_apply_list')->where('id',$sale_apply_id)->find();        $user = Db::name('user')->where('userId',$sale_apply_list['userId'])->field('nickName,userMobile,userBalance,userIntegral')->find();        if($examine_type == 2){            //拒绝            $all = [                'examine_type' => 2,                'refuse_reason' => $refuse_reason,                'examine_time' => time()            ];            if($sale_apply_list['type'] == 1){                //增减余额                if($sale_apply_list['status'] == 1){                    $way = '增加';                }else{                    $way = '扣除';                }                $catch['behavior'] = '值班经理拒绝了你'.$way.$user['nickName'].$sale_apply_list['amount'].'余额的申请';            }elseif($sale_apply_list['type'] == 2){                //增减积分                if($sale_apply_list['status'] == 1){                    $way = '增加';                }else{                    $way = '扣除';                }                $catch['behavior'] = '值班经理拒绝了你'.$way.$user['nickName'].$sale_apply_list['amount'].'积分的申请';            }elseif($sale_apply_list['type'] == 3){                //赠送优惠券                $catch['behavior'] = '值班经理拒绝了你赠送'.$user['nickName'].'一张优惠券的申请';            }elseif($sale_apply_list['type'] == 4){                //赠送抵扣券                $catch['behavior'] = '值班经理拒绝了你赠送'.$user['nickName'].'一张抵扣券的申请';            }else{                //赠送充值卡                $catch['behavior'] = '值班经理拒绝了你赠送'.$user['nickName'].'一张虚拟充值卡的申请';            }            $data = Db::name('sale_apply_list')->where('id',$sale_apply_id)->update($all);            if($data){                $catch = array(                    'userId' => $sale_apply_list['user_admin_id'],                    'quUserId' => $sale_apply_list['userId'],                    'addTime' => time(),                    'note' => $sale_apply_list['notes'],                    'examine_people' => $userInfo['user_admin_id'],                    'behavior' => $catch['behavior'],                );                Db::name('operate_log')->insertGetId($catch);                return json(array('code' => 200, 'msg' => 'success:成功'));            }else{                return json(array('code' => 500, 'msg' => 'success:失败','data'=>$data));            }        }else{         //同意            $all = [                'examine_type' => 1,                'examine_time' => time()            ];            $data = Db::name('sale_apply_list')->where('id',$sale_apply_id)->update($all);            if($data){                if($sale_apply_list['type'] == 1){                    //增减余额                    if($sale_apply_list['status'] == 1){                        $amount = $sale_apply_list['amount'];                        $way = '增加';                    }else{                        if($sale_apply_list['amount'] > $user['userBalance']){                            return json(array('code' => 500, 'msg' => '扣除的金额不能大于用户余额'));                        }                        $amount = $sale_apply_list['amount']*(-1);                        $way = '扣除';                    }                    $catch['behavior'] = '值班经理同意了你'.$way.$user['nickName'].$sale_apply_list['amount'].'余额的申请';                    $add = Db::name('user')->where('userId', '=', $sale_apply_list['userId'])->setInc('userBalance', $amount);                }elseif($sale_apply_list['type'] == 2){                    //增减积分                    if($sale_apply_list['status'] == 1){                        $amount = $sale_apply_list['amount'];                        $way = '增加';                    }else{                        if($sale_apply_list['amount'] > $user['userIntegral']){                            return json(array('code' => 500, 'msg' => '扣除的积分不能大于用户积分'));                        }                        $amount = $sale_apply_list['amount']*(-1);                        $way = '扣除';                    }                    $catch['behavior'] = '值班经理同意了你'.$way.$user['nickName'].$sale_apply_list['amount'].'积分的申请';                    $add = Db::name('user')->where('userId', '=', $sale_apply_list['userId'])->setInc('userIntegral', $amount);                }elseif($sale_apply_list['type'] == 3){                    //赠送优惠券                    $coupon = Db::name('coupon')->where('couponId', '=', $sale_apply_list['coupons_id'])->find();                    if (empty($coupon)) {                        return json(array('code' => 500, 'msg' => '优惠券数据出错，请联系管理员处理'));                    }                    $endTime = 0;                    if ($coupon['validTime'] > 0) {                        $endTime = strtotime(date("Y-m-d",strtotime("+".$coupon['validTime']." day")));                    }                    $catch = array(                        'couponId' => $coupon['couponId'],                        'userId' => $sale_apply_list['userId'],                        'addTime' => strtotime(date('Y-m-d', time())),                        'endTime' => $endTime,                    );                    $catch['behavior'] = '值班经理同意了你赠送'.$user['nickName'].'一张优惠券的申请';                    $add = Db::name('user_coupon')->insertGetId($catch);                }elseif($sale_apply_list['type'] == 4){                    //赠送抵扣券                    $voucher = Db::name('voucher')->where('voucherId', '=', $sale_apply_list['coupons_id'])->find();                    if (empty($voucher)) {                        return json(array('code' => 500, 'msg' => '抵扣券数据出错，请联系管理员处理'));                    }                    $catch = array(                        'voucherId' => $voucher['voucherId'],                        'userId' => $sale_apply_list['userId'],                        'addTime' => strtotime(date('Y-m-d', time())),                    );                    $catch['behavior'] = '值班经理同意了你赠送'.$user['nickName'].'一张抵扣券的申请';                    $add = Db::name('user_voucher')->insertGetId($catch);                }else{                    //赠送充值卡                    $card_set = Db::name('card_set')->where('id', '=', $sale_apply_list['coupons_id'])->find();                    if (empty($card_set)) {                        return json(array('code' => 500, 'msg' => '充值卡数据出错，请联系管理员处理'));                    }                    $catch = array(                        'denomination' => $card_set['amount'],                        'number' => 1,                        'userMobile'=>$user['userMobile'],                        'types'=>1,                        'substantialCharge'=>$card_set['reality_amount'],                    );                    $model = new \app\mingteng\model\Rechargeable();                    $catch['behavior'] = '值班经理同意了你赠送'.$user['nickName'].'一张虚拟充值卡的申请';                    $add = $model->virtually($catch,$sale_apply_list['userId']);                }                if($add){                    $catch = array(                        'userId' => $sale_apply_list['user_admin_id'],                        'quUserId' => $sale_apply_list['userId'],                        'addTime' => time(),                        'note' => $sale_apply_list['notes'],                        'examine_people' => $userInfo['user_admin_id'],                        'behavior' => $catch['behavior'],                    );                    Db::name('operate_log')->insertGetId($catch);                    return json(array('code' => 200, 'msg' => 'success:成功'));                }else{                    return json(array('code' => 500, 'msg' => 'success:失败'));                }            }else{                return json(array('code' => 500, 'msg' => 'success:失败','data'=>$data));            }        }    }    /**     * 销售或经理扫码获取抵扣券核销信息     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function scan_code(){        $token = input('token', '');        $data= $this->request->param('data/s');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if(empty($data)){            return json(array('code' => 500, 'msg' => 'success:参数错误'));        }        $voucher_qrcode = Db::name('voucher_qrcode')->alias('a')            ->join('voucher v','v.voucherId = a.voucherId','LEFT')            ->where(['a.encrData'=>$data,'a.type'=>0])            ->field('a.*,v.voucherName,v.voucherAmount')            ->find();        return json(array('code' => 200, 'msg' => 'success:成功','data'=>$voucher_qrcode));    }    /**     * 销售或经理确认核销     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function write_off(){        $token = input('token', '');        $userId = input('userId', '');        $voucher_qrcode_id = input('voucher_qrcode_id', '');        $userInfo = $this->checklogin($token);        if (empty($userInfo)) {            return json(array('code' => 403, 'msg' => 'error:no login,未登录'));        }        if(empty($userId)){            return json(array('code' => 500, 'msg' => 'success:用户id不能为空'));        }        if(empty($voucher_qrcode_id)){            return json(array('code' => 500, 'msg' => 'success:voucher_qrcode_id不能为空'));        }        $user = Db::name('user')->where(['userId'=>$userId])->find();        if(empty($user)){            return json(array('code' => 500, 'msg' => 'success:该用户不存在'));        }        $voucher_qrcode = Db::name('voucher_qrcode')->where('id',$voucher_qrcode_id)->find();        if(empty($voucher_qrcode)){            return json(array('code' => 500, 'msg' => 'success:该抵扣券不存在'));        }        $add = [            'userId' => $userId,            'voucherId' => $voucher_qrcode['voucherId'],            'addTime' => time(),        ];        $data = Db::name('user_voucher')->insert($add);        if($data){            $up = Db::name('voucher_qrcode')->where('id',$voucher_qrcode_id)->update(['type' => 1]);            if($up){                return json(array('code' => 200, 'msg' => 'success:成功','data'=>$up));            }else{                return json(array('code' => 500, 'msg' => 'success:失败','data'=>$up));            }        }else{            return json(array('code' => 500, 'msg' => 'success:失败','data'=>$data));        }    }    /**     * 抵扣券使用限制数量     * @return \think\response\Json     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function voucher_set(){        $data = Db::name('voucher_limitations_set')->where('id',1)->field('num')->find();        return json(array('code' => 200, 'msg' => 'success:成功','data'=>$data));    }}